package org.bankabeule.ui.application;

import org.bankabeule.ui.actions.CloseAction;
import org.bankabeule.ui.actions.ImportOFXAction;
import org.bankabeule.ui.actions.NewAction;
import org.bankabeule.ui.actions.OpenAction;
import org.bankabeule.ui.actions.SaveAction;
import org.bankabeule.ui.actions.SaveAsAction;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.internal.registry.ActionSetRegistry;
import org.eclipse.ui.internal.registry.IActionSetDescriptor;

@SuppressWarnings("restriction")
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

	private IAction newAction;
	private IAction openAction;
	private IAction saveAction;
	private IAction saveAsAction;
	private IAction closeAction;
	private IWorkbenchAction closeEditorAction;
	private IWorkbenchAction closeAllEditorsAction;
	private IAction importAction;
	private IWorkbenchAction exitAction;
	private IWorkbenchAction aboutAction;
	private IWorkbenchAction newWindowAction;

	/**
	 * @param configurer
	 */
	public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
	}

	/**
     * 
     */
	protected void makeActions(IWorkbenchWindow window) {

		ActionSetRegistry reg = WorkbenchPlugin.getDefault().getActionSetRegistry();
		IActionSetDescriptor[] actionSets = reg.getActionSets();
		for (int i = 0; i < actionSets.length; i++) {
			IExtension ext = actionSets[i].getConfigurationElement()
					.getDeclaringExtension();
			reg.removeExtension(ext, new Object[] { actionSets[i] });
		}

		newAction = new NewAction();
		register(newAction);

		openAction = new OpenAction();
		register(openAction);

		saveAction = new SaveAction();
		register(saveAction);

		saveAsAction = new SaveAsAction();
		register(saveAction);

		closeAction = new CloseAction();
		register(closeAction);

		closeEditorAction = ActionFactory.CLOSE.create(window);
		closeEditorAction.setText("Close editor");
		register(closeEditorAction);

		closeAllEditorsAction = ActionFactory.CLOSE_ALL.create(window);
		closeAllEditorsAction.setText("Close all editors");
		register(closeAllEditorsAction);

		importAction = new ImportOFXAction();
		register(importAction);

		exitAction = ActionFactory.QUIT.create(window);
		register(exitAction);

		aboutAction = ActionFactory.ABOUT.create(window);
		register(aboutAction);

		newWindowAction = ActionFactory.OPEN_NEW_WINDOW.create(window);
		register(newWindowAction);

	}

	protected void fillMenuBar(IMenuManager menuBar) {
		MenuManager fileMenu = new MenuManager("&File", "bankabeule.menu.file");
		MenuManager windowsMenu = new MenuManager("&Windows",
				"bankabeule.menu.window");
		MenuManager helpMenu = new MenuManager("&Help", "bankabeule.menu.help");

		menuBar.add(fileMenu);
		// Add a group marker indicating where action set menus will appear.
		menuBar.add(windowsMenu);
		menuBar.add(helpMenu);

		fileMenu.add(newAction);
		fileMenu.add(openAction);
		fileMenu.add(saveAction);
		fileMenu.add(saveAsAction);
		fileMenu.add(closeAction);
		fileMenu.add(new Separator());
		fileMenu.add(closeEditorAction);
		fileMenu.add(closeAllEditorsAction);
		fileMenu.add(new Separator());
		fileMenu.add(importAction);
		fileMenu.add(new Separator());
		fileMenu.add(exitAction);

		// Windows
		windowsMenu.add(newWindowAction);

		// Help
		helpMenu.add(aboutAction);
	}

	@Override
	protected void fillCoolBar(ICoolBarManager coolBar) {
		ToolBarManager fileToolbar = new ToolBarManager();

		coolBar.removeAll();
		coolBar.add(fileToolbar);

		fileToolbar.add(newAction);
		fileToolbar.add(openAction);
		fileToolbar.add(saveAction);
		fileToolbar.add(saveAsAction);
		fileToolbar.add(closeAction);
		fileToolbar.add(new Separator());
		fileToolbar.add(importAction);
	}

}
